package com.hisense.hiioc.hismartplate.utils;

import io.netty.channel.Channel;

import java.net.SocketAddress;

/**
 * @Author: almo
 * @Date: 2018/12/5 下午3:30
 * @Version 1.0
 */
public class NettyUtils {

    /**
     * 获取Channel的远程IP地址
     *
     * @param channel
     * @return
     */
    public static String parseChannelRemoteAddr(final Channel channel) {
        if (null == channel) {
            return "";
        }
        SocketAddress remote = channel.remoteAddress();
        final String addr = remote != null ? remote.toString() : "";

        if (addr.length() > 0) {
            int index = addr.lastIndexOf("/");
            if (index >= 0) {
                return addr.substring(index + 1);
            }
            return addr;
        }
        return "";
    }
}
